/**
 * @author Xiao Xin
 * @version 1.0.0
 * @date 20210409
 * @copyright MIN-Group:国家重大科技基础设施-未来网络北大实验室：深圳市信息论与未来网络重点实验室
 */
 var SelfEncodingBase = require('../encoding/SelfEncodingBase').SelfEncodingBase
 var Encoder = require('../encoding/Encoder').Encoder
 var Block = require('../encoding/Block').Block
 var TLV = require('../encoding/TLV')
 var {TlvComponentBase,InitialAble} = require('./TlvComponentBase')
 var aggregation = require('../common/aggregation')//实现了多继承
 var abAndStr = require('../common/abAndStr')//arrayBuffer和String类型的互换

//
// 表示一个控制命令参数 Uri
//
// @Description:
//
class ControlParameterLocalUri extends aggregation(TlvComponentBase,InitialAble,SelfEncodingBase){
  constructor(){
    super()
    this.localUri = ""
  }

  getLocalUri(){
    return this.localUri;
  }

  setLocalUri(localUri){
    if(localUri instanceof String){
      this.localUri = localUri
      this.doInitial()
    }else{
      this.localUri = localUri+""
      this.doInitial()
    }//判断传进来的uri是否是字符串，如果是则直接赋值给uri，不是则转化为字符类型再赋值
  }

  // 将 ControlParameterLocalUri 线速编码为一个TLV
  //
  // @Description:
  // @receiver c
  // @param encoder{Encoder}
  // @return int
  // @return error
  //
  wireEncode(encoder){
    var totalLength = 0
    try {
    //编码 TLV-VALUE
    var strBytes = abAndStr.str2ab(this.localUri)
    // string类型转化为arraybuffer类型，js中的string类型是utf-16
    var tmpLen = encoder.prependByteArray(strBytes,BigInt(strBytes.byteLength))
    totalLength += tmpLen 

    // 编码 TLV-LENGTH
    tmpLen = encoder.prependVarNumber(BigInt(totalLength))
    totalLength += tmpLen

    // 编码 TLV-TYPE
    tmpLen = encoder.prependVarNumber(TLV.ComponentType.TlvManagementLocalUri)
    totalLength += tmpLen

    return totalLength
    } catch (error) {
      throw error
    }
  }

  //
  // 从 TLV Block 中解码出一个 ControlParameterlocalUri
  //
  // @Description:
  // @receiver c
  // @param block{Block}
  // @return error
  //
  wireDecode(block){
    try {
        // 检查 Type 是否正确
        TLV.expectType(block.getType(), TLV.ComponentType.TlvManagementLocalUri)
        var value = abAndStr.ab2str(block.getValue())
        // arraybuffer转化为string
        this.setLocalUri(value)
    } catch (error) {
      throw error
    }
  }
}

module.exports = {
  ControlParameterLocalUri:ControlParameterLocalUri
}